package uk.co.richardwkirk.parkrun.android.news;

import uk.co.richardwkirk.parkrun.android.IntentExtras;
import uk.co.richardwkirk.parkrun.android.R;
import uk.co.richardwkirk.parkrun.android.dataload.cache.NewsCache;
import uk.co.richardwkirk.parkrun.news.NewsFeed;
import uk.co.richardwkirk.parkrun.news.NewsItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;

public class NewsContentDisplay extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_content_display);
		
		// Have we already loaded the news feed?
		showNewsItem();
	}
	
	private void showNewsItem() {
		// Get the news subject
		Intent intent = getIntent();
		
		// Get the news feed
		String newsFeedUrl = intent.getStringExtra(IntentExtras.EXTRA_NEWS_FEED_URL);
		NewsFeed newsFeed = NewsCache.getSingleton().getItem(newsFeedUrl);
		
		// Get the new item number
		int newsItemNumber = intent.getIntExtra(IntentExtras.EXTRA_NEWS_ITEM_NUMBER, 0);
		
		// Display the news item
		showNewsItem(newsFeed, newsItemNumber);
	}


	/**
	 * Update the news in the WebView
	 */
	public void showNewsItem(NewsFeed news, int newsItemNumber) {
		WebView newsWebView = (WebView) findViewById(R.id.news_webview);
		NewsItem newsItem = news.getItem(newsItemNumber);
		String newsItemHtml = newsItem.getHtmlContent();
		//Log.d("PARKRUN", "News item html: [" + newsItemHtml + "]");
		newsWebView.loadData(newsItemHtml, "text/html; charset=UTF-8", null);
		this.setTitle(newsItem.getTitle());
	}
	

}
